package org.example.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.User;
import org.example.entity.Audit;
import org.example.exception.ServiceException;
import org.example.mapper.UserMapper;
import org.example.utils.TokenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper ,User> {

    @Resource
    UserMapper userMapper;
    @Resource
    AuditService auditService;

    @Override
    public boolean save(User entity) {      //重写父类的方法
        if(StrUtil.isBlank(entity.getName())){
            entity.setName(entity.getUsername());
        }
        if(StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("123");
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("学生");
        }
        return super.save(entity);
    }

    //    public void insertUser(User user){
//        userMapper.insert(user);
//
//    }
//
//    public void updateUser(User user) {
//        userMapper.updateUser(user);
//    }
//
//    public void deleteUser(Integer id) {
//        userMapper.deleteUser(id);
//    }
//
//    public void batchDeleteUser(List<Integer> ids) {
//        for (Integer id : ids) {
//            userMapper.deleteUser(id);
//        }
//    }
//
//    public List<User> selectAll() {
//        return userMapper.selectAll();
//    }
//
//    public User selectById(Integer id) {
//        return userMapper.selectById(id);
//    }
//
//    public List<User> selectByName(String name) {
//        return userMapper.selectByName(name);
//    }
//
//
//    public List<User> selectByMore(String username, String name) {
//        return userMapper.selectByMore(username,name);
//    }
//
//    public List<User> selectByMo(String username, String name) {
//        return userMapper.selectByMo(username,name);
//    }
//
//    public  Map<String, Object> selectByPage(Integer pageNum,Integer pageSize,String username, String name) {
//        Integer skipNum=(pageNum-1) *pageSize;  //计算出来
//        Map<String, Object> result= new HashMap<>();
//        List<User> userList = userMapper.selectByPage( skipNum,pageSize,username ,name);
//        List<User> total = userMapper.selectCountByPage(username,name);
//        result.put("list",userList);
//        result.put("total",total);
//        return result;
//    }
    //验证用户账户是否合法

    public User selectByUsername(String username){         //条件查询构造器
        QueryWrapper<User> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("username",username);   //eq=> == where username=#{username}
        //根据用户名查询数据库用户信息
        return getOne(queryWrapper);     // select * from user where username=#{username}
    }
    public User login(User user) {
        User dbUser = selectByUsername((user.getUsername()));
        if(dbUser == null){
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new ServiceException("用户名或密码错误");
        }
        //token create
        String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    public User register(User user) {
        User dbUser = selectByUsername((user.getUsername()));
        if (dbUser != null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }
        User dbUser2 = selectByUsername2((user.getName()));
        if (dbUser != null) {
            // 抛出一个自定义的异常
            throw new ServiceException("名字已存在");
        }
        user.setName(user.getName());
        user.setPhone("123");
        userMapper.insert(user);
        return user;
    }

    private User selectByUsername2(String name) {
        return userMapper.selectByUsername2(name);
    }


    public void resetPassword(User user) {
        User dbUser = selectByUsername((user.getUsername()));
        if(dbUser == null){
            //抛出一个自定义的异常
            throw new ServiceException("用户不存在");
        }
        if(!user.getPhone().equals(dbUser.getPhone())){
            throw new ServiceException("验证错误");
        }
        dbUser.setPassword("123");   //重置密码
        updateById(dbUser);
    }


    @Transactional
    public void updateUserAndAudit(User user) {
        // 1. 更新用户表数据
        userMapper.updateById(user);


    }

    @Transactional
    public  List<User>  selectRole(String role){
        return userMapper.selectRole(role);
    }


    public List<User> findUsersByNameLike(String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", name);
        return baseMapper.selectList(queryWrapper);
    }

    public User findUserById(int teacherId) {
        return userMapper.findById(teacherId);
    }

    public List<User> selectbyclassname(String className,String name) {
       return userMapper.selectbyclassname(className,name);

    }

    public List<User> AvatarUrl(String username) {
        return userMapper.AvatarUrl(username);
    }

    public String selectbyId(Integer valueOf) {
        return userMapper.selectbyId(valueOf);
    }


}
